<?php
namespace Home\Controller;

use Think\Controller;

class ShareController extends BaseController
{

    /**
     * 获取分享活动列表
     */
    public function getShareList()
    {
        $memberId = parent::get('account')['id'];
        $page = (int)I('p') ? (int)I('p') : 0;
        $list = 10;
        $shareList = M("share")
            ->query("SELECT distinct(s.id) id1,s.*,m.memberId FROM t_share s LEFT JOIN t_myshare m ON m.shareId = s.id 
            WHERE s.isDelete=2 and '".time()."'> UNIX_TIMESTAMP(s.`start`) AND 
            '".time()."'<= UNIX_TIMESTAMP(s.`end`) limit ".$page*$list.",".$list);
        if (!empty($shareList)) {
            foreach ($shareList as $k=>$v) {
                $shareList[$k]['start'] = date("Y-m-d", strtotime($v['start']));
                $shareList[$k]['end'] = date("Y-m-d", strtotime($v['end']));
                if (!empty($memberId) && $memberId == $v['memberid']) {
                    $shareList[$k]['is_paticipate'] = 1;
                } else {
                    $shareList[$k]['is_paticipate'] = 0;
                }
            }
        }
        $this->ajaxSuccess($shareList);
    }

    //获取活动详情
    public function getShareDetail(){
        $id = (int)I('get.id');
        $memberId = parent::get("account")['id'];
        if (empty($id)) $this->ajaxError("参数错误：未知的活动id！");
        $detail = M("share")
            ->where(array(
                "id"=>$id,
                "isDelete"=>2,
            ))
            ->find();
        if (!$detail) {
            $this->ajaxError("活动不存在或已删除！");
        }
        if (!empty($detail['imgs'])) {
            $img_arr = explode(',', $detail['imgs']);
            $detail['img_arr'] = $img_arr;
        } else {
            $detail['img_arr'][0] = $detail['img'];
        }
        $detail['start'] = date("Y-m-d", strtotime($detail['start']));
        $detail['end'] = date("Y-m-d", strtotime($detail['end']));
        $detail['html'] = htmlspecialchars_decode($detail['html']);
        $detail['is_paticipate'] = 0;

        //是否已参加
        if ($memberId) {
            $is_paticipate = M("myshare")->where(array("memberId"=>$memberId, "shareId"=>$id))->find();
            if ($is_paticipate) {
                $detail['is_paticipate'] = 1;
            }
        }

        //浏览量+1
        M("share")->where(array("id"=>$id))->setInc("readNum", 1);
        return $this->ajaxSuccess(array('detail'=>$detail));
    }

    //共享活动报名
    public function signUp()
    {
        parent::loginCheck();

        //身份信息是否完善
        A("member")->satisfyInformation();

        $memberId = parent::get('account')['id'];
        $id = (int)I('get.id');
        if (empty($id)) $this->ajaxError("参数错误：未知的活动id！");
        $detail = M("share")
            ->where(array(
                "id"=>$id,
                "isDelete"=>2,
            ))
            ->find();
        if (!$detail) {
            $this->ajaxError("活动不存在或已删除！");
        }
        if (time() < strtotime($detail['start'])) {
            $this->ajaxError("活动还未开始！");
        }
        if (time() >= strtotime($detail['end'])) {
            $this->ajaxError("活动已结束！");
        }

        //是否已报名
        $check = M('myshare')->where(array("memberId"=>$memberId, "shareId"=>$id))->find();
        if ($check) {
            $this->ajaxError("您已报名此活动！");
        }

        if (M('myshare')->add(array("memberId"=>$memberId, "shareId"=>$id, "createTime"=>now_datetime()))) {
            $this->ajaxSuccess("报名成功！");
        } else {
            $this->ajaxError("操作失败");
        }
    }

    /**
     * 我参与的共享列表
     */
    public function myShare()
    {
        parent::loginCheck();

        $page = (int)I('p') ? (int)I('p') : 0;
        $list = 10;

        $memberId = parent::get('account')['id']; //当前用户id
        $myCustomized = M("myshare")
            ->alias("m")
            ->field("m.*,s.name,s.img,s.start,s.end")
            ->join("left join t_share s ON m.shareId=s.id")
            ->where(array("memberId"=>$memberId))
            ->limit($page*$list, $list)->select();

        $this->ajaxSuccess($myCustomized);
    }

    /**
     * 我的还款
     */
    public function myReminders()
    {
        parent::loginCheck();
        $memberId = parent::get("account")['id'];
        $page = (int)I('p') ? (int)I('p') : 0;
        $list = 10;
        $status = (int)I('state');

        $where = "1=1";
        if (empty($status) || 0 == $status) {
            $where .= " AND memberId = '".$memberId."'";
        } else {
            $where .= " AND memberId = '".$memberId."' AND state='".$status."'";
        }

        $list = M('myreminders')->where($where)->limit($page*$list, $list)->select();

        if (!empty($list)) {
            foreach ($list as $k=>$v) {
                if (1 == $v['state']) {
                    $list[$k]['state_str'] = "未还款";
                } else if (2 == $v['state']) {
                    $list[$k]['state_str'] = "已还款";
                }
            }
        }

        $this->ajaxSuccess($list);
    }
}
